Dynomotion

Group: DynoMotion Message: 10741 From: tmday7 Date: 12/30/2014
Subject: Adding Coolant On/Off button to main dialog
Hi Tom,
 Trying to add another IDC_ button to the main dialog face to toggle the coolant pump.
I was able to get the code correct for 2 buttons, one turns on the other off. But now iam trying to merge these 2 actions into one button.
The below code is what i have tried. When i click my button the coolant pump turns on but will not turn off when i click button again.

void CKMotionCNCDlg::OnBnClickedOnCoolant()
{
    if (Interpreter->InvokeAction(9,FALSE))   // do the defined action for M9 Code
    Interpreter->InvokeAction(9,FALSE);
    else
    Interpreter->InvokeAction(8,FALSE);// do the defined action for M8 Code
}

Is this because the Interpreter is not seeing what state the pump I/O is in?
Was trying to copy the user C program that toggles a bit. :)

Thanks,
Troy


Group: DynoMotion Message: 10743 From: tmday7 Date: 12/30/2014
Subject: Re: Adding Coolant On/Off button to main dialog
Also wondering if this button added to the main dialog face can execute a user C program like the User Buttons do only coded into the KMotionCNCDlg?

Thanks,
Troy
Group: DynoMotion Message: 10746 From: eric_kato_sanders Date: 12/31/2014
Subject: Re: Adding Coolant On/Off button to main dialog
The way you want to do this is by using the MFC function of IsDlgButtonChecked(IDC_XXXX) to see if the button state is currently pressed.  Replace the IDC_XXXX with what you have defined as the ID for that button.
This function works for check boxes and radio buttons as well.

void CKMotionCNCDlg::OnBnClickedOnCoolant()
{
    if( IsDlgButtonChecked(IDC_XXXX)
    {
        Interpreter->InvokeAction(8,FALSE);
    }
    else
    {
        Interpreter->InvokeAction(9,FALSE);
    }
}
Group: DynoMotion Message: 10747 From: tmday7 Date: 12/31/2014
Subject: Re: Adding Coolant On/Off button to main dialog
Hello Eric,
 Is there any other code i need to add with what you posted?  The button is not turning on the pump. But if i do a MDI to turn on coolant pump (M08) and then click my coolant button then pump will turn off.

Thanks for any info,
Troy
Group: DynoMotion Message: 10750 From: eric_kato_sanders Date: 1/1/2015
Subject: Re: Adding Coolant On/Off button to main dialog
You shouldn't need anything else.  I see that I forgot a closing bracket on this line:
if( IsDlgButtonChecked( IDC_XXXX)
it should be:

if( IsDlgButtonChecked( IDC_XXXX))


It sounds like that line is always returning the same value.  I bet that you have the button setup
currently so that after you click the button, it becomes unlicked.  In order to make the button "toggle" on and off, you need to use the UpdateUI function.
You will need to do a few things.  You need to create a new variable, initialize it to a starting value, toggle it when you click the button, and then create a function to handle making the button stay pushed down.

In the KMotionCNCDlg.h file, declare a variable to hold the current button state.

bool PersistRestored;
bool m_bFloodOn;

In the KMotionCNCDlg.cpp file, in the constructor function initialize the new variable:

CKMotionCNCDlg::CKMotionCNCDlg(CWnd* pParent /*=NULL*/)
    : CDlgX(pParent)
{

   m_bFloodOn=FALSE;


In the message map section setup the message handler.  Below the line:
BEGIN_MESSAGE_MAP(CKMotionCNCDlg, CDlgX)

Add a line:
ON_UPDATE_COMMAND_UI(IDC_XXXX, OnUpdateFlood)

In the KMotionCNCDlg.h file, towards the bottom where you see declarations starting with
afx_msg void ...

declare the new function by adding the line
afx_msg void OnUpdateFlood(CCmdUI* pCmdUI);

In the body of the KMotionCNCDlg.cpp file, add the new function to toggle the button state:

void CKMotionCNCDlg::OnUpdateFlood(CCmdUI* pCmdUI)
{
    pCmdUI->SetCheck(m_bFloodOn);
}

Finally, change your button handler to:

void CKMotionCNCDlg: :OnBnClickedOnCo olant()
{
if(!m_bFloodOn)
{
Interpreter- >InvokeAction (8,FALSE) ;
}
else
{
Interpreter- >InvokeAction (9,FALSE) ;

m_bFloodOn = !m_bFloodOn

}

Group: DynoMotion Message: 10751 From: tmday7 Date: 1/1/2015
Subject: Re: Adding Coolant On/Off button to main dialog
Hi Eric,
Fixed the closing bracket yesterday.

I added the button state code, but there still is nothing happening when i click button. Button does not stay clicked nor does the pump turn on when clicked.
Have not even cut my teeth with C programming, so below is what i added to files and the area in each file where i added the lines of code. Did i mis something?

KMotionCNCDlg.h
1)Add line of code.....
bool m_bFloodOn;  Below this .....class CKMotionCNCDlg : public CDlgX
{
// Construction
public:

2)Add line of code...
afx_msg void OnUpdateFlood(CCmdUI* pCmdUI); Below this....public:

KmotionCNCDlg.cpp
1)Add line of code...
m_bFloodOn=FALSE; Below this... CKMotionCNCDlg::CKMotionCNCDlg(CWnd* pParent /*=NULL*/)
    : CDlgX(pParent)
{  


2)Add line of code....ON_UPDATE_COMMAND_UI(IDC_OnCoolant, OnUpdateFlood) Below this....BEGIN_MESSAGE_MAP(CKMotionCNCDlg, CDlgX)
    //{{AFX_MSG_MAP(CKMotionCNCDlg)

3)Add code....void CKMotionCNCDlg::OnUpdateFlood(CCmdUI* pCmdUI)
{
    pCmdUI->SetCheck(m_bFloodOn);
}

Below this.....void CKMotionCNCDlg::OnBnClickedSpindleoff()
{
    Interpreter->InvokeAction(5,FALSE);  // do the defined action for M Code
}

4) Add code....void CKMotionCNCDlg::OnBnClickedOnCoolant()
{
    if(!m_bFloodOn)
    {
    Interpreter->InvokeAction(8,FALSE);
    }
    else
    {
    Interpreter->InvokeAction(9,FALSE);
    }
    m_bFloodOn = !m_bFloodOn;
}
Below this....void CKMotionCNCDlg::OnUpdateFlood(CCmdUI* pCmdUI)
{
    pCmdUI->SetCheck(m_bFloodOn);
}

I did not get any rebuild errors after i added ";" at the end of
....
pCmdUI->SetCheck(m_bFloodOn);

Thanks again,
Troy

Group: DynoMotion Message: 10753 From: Tom Kerekes Date: 1/1/2015
Subject: Re: Adding Coolant On/Off button to main dialog
Hi Troy,

It isn't clear to me what kind of GUI "button" you added.  Is it a regular button?  A bitmap Button? Toggle button?  a checkbox?

I think you want something that has and shows a state like a Toggle Button (such as the Keyboard Jog Button) or a checkbox like the simulate checkbox.  You might have stated that earlier but I missed it.  A checkbox would be the easiest.

Also I don't think you want or need a state variable like m_bFloodOn.  That would work if only your button was controlling the state, but in this case other M Codes and such can change the state of the Flood on/off.  Trying to maintain your own state in your own variable is likely to get out of sync with reality.  I think it would be better to reach into the Interpreter state to check whether the Flood is on or off to both display the button state and to decide whether a button click should turn the Flood on or off.

The internal Interpreter state variables are defined in this header file:

\GCodeInterpreter\rs274ngc.h

The one for Flood is defined:

    ON_OFF flood;        // whether flood coolant is on

From within the KMotionCNCDlg.c file it can be accessed as

Interpreter->p_setup->flood

Try using that instead.

Another issue is that if something else (ie GCode) turns the coolant on or off I'm not sure that the screen will automatically update to show the new state.  We will probably have to add something in the OnTimer function to make sure it does much like the DROs, GCode Units, etc update periodically.

HTH
Regards
TK



Group: DynoMotion Message: 10754 From: tmday7 Date: 1/1/2015
Subject: Re: Adding Coolant On/Off button to main dialog
Hi Tom,
I started with making a copy of the spindle off button.Later was going to try making a button like the jog toggle.
But the checkbox would work too, and less buttons on screen.
I'll give your instructions a go tonight.
Thanks again,
Troy
Group: DynoMotion Message: 10755 From: tmday7 Date: 1/1/2015
Subject: Re: Adding Coolant On/Off button to main dialog
Hi Tom,
 Dont know if iam understanding this correctly, but this is what i have done.

In KMotionCNCDlg.h i added the following....
1)
 afx_msg void OnBnClickedOnCoolant();
 afx_msg void OnBnClickedOffCoolant();

below....
public:
at bottom of last afx list.
_____________________________________________
In KMotionCNCDlg.cpp i added the following....
1)
ON_BN_CLICKED(IDC_OnCoolant, &CKMotionCNCDlg::OnBnClickedOnCoolant)
    //ON_COMMAND(ID_??, OnBnClickedOnCoolant)
    ON_BN_CLICKED(IDC_OffCoolant, &CKMotionCNCDlg::OnBnClickedOffCoolant)
    //ON_COMMAND(ID_??, OnBnClickedOffCoolant)

below...
BEGIN_MESSAGE_MAP(CKMotionCNCDlg, CDlgX)
    //{{AFX_MSG_MAP(CKMotionCNCDlg)

at bottom of this message map list.
_________________________________________

2)
void CKMotionCNCDlg::OnBnClickedOnCoolant()
{
    if (Interpreter->p_setup->flood)
    Interpreter->InvokeAction(9,FALSE);// do the defined action for M9 Code
    else
    Interpreter->InvokeAction(8,FALSE); // do the defined action for M8 Code
}
void CKMotionCNCDlg::OnBnClickedOffCoolant()
{
    Interpreter->InvokeAction(9,FALSE);   // do the defined action for M9 Code
}

below....
void CKMotionCNCDlg::OnBnClickedSpindleoff()
{
    Interpreter->InvokeAction(5,FALSE);  // do the defined action for M Code
}
________________________________________________________

Now button works like this....
If i MDI M08 then button will turn OFF pump but wont turn it on.
If i MDI M09 then button will turn ON pump but wont turn it off.

Is this where the OnTimer function you mentioned comes into play?

Wish i would have started working with KmotionCNC like this when i got the first KFLOP back in 08 or 09. Could be a little more familiar with coding it. :)

Thanks,
Troy
Group: DynoMotion Message: 10756 From: Tom Kerekes Date: 1/1/2015
Subject: Re: Adding Coolant On/Off button to main dialog
Hi Troy,

The M8 and M9 (from the MDI) should (and need to) work properly even before making any changes to the source code to add buttons because your code additions are basically just calling those.

How are M8 and M9 configured?

Regards
TK


Group: DynoMotion Message: 10757 From: tmday7 Date: 1/1/2015
Subject: Re: Adding Coolant On/Off button to main dialog
Hi Tom,
 The M08 and M09 do work correctly , in a Gcode program or in a MDI.
I have M8 and M9 configured in the ToolSetup>M3-M9 tab as follows...
M8 (I/O bit) (Set Bit 44) to (1)
M9 (I/O bit) (Set Bit 44) to (0)

And in my main INIT C i have ....

main()
{
    SetBitDirection(44,1); //Coolant Pump
    SetStateBit(44,0);


    #define DROIN 40
    double *pin  = (double *)&persist.UserData[(DROIN -1)*2];
   
    int result;

    Manual = !ReadBit(MANUALBIT);

    ch0->InputMode=ENCODER_MODE;
    ch0->OutputMode=CL_STEP_DIR_MODE;
    ch0->Vel=25000;

Thanks,
Troy
Group: DynoMotion Message: 10758 From: Tom Kerekes Date: 1/1/2015
Subject: Re: Adding Coolant On/Off button to main dialog
Hi Troy,

Ah didn't read carefully you push your new button after the MDI commands.  That all looks correct.

Regarding the code additions: it seems you are using two separate buttons - one to turn on and one to turn off.  And there will be no indication or display if the Flood is on or off on the screen.  If so that makes everything simple.  There is no need to check the Flood on/off state at all.  The ON button unconditionally will always turn it on (M8).  The OFF button will always turn it off (M9).  So change:

void CKMotionCNCDlg::OnBnClickedOnCoolant()
{
    if (Interpreter->p_setup->flood)
    Interpreter->InvokeAction(9,FALSE);// do the defined action for M9 Code
    else
    Interpreter->InvokeAction(8,FALSE); // do the defined action for M8 Code
}

to

void CKMotionCNCDlg::OnBnClickedOnCoolant()
{
    Interpreter->InvokeAction(8,FALSE); // do the defined action for M8 Code
}

Regards
TK
Group: DynoMotion Message: 10759 From: tmday7 Date: 1/1/2015
Subject: Re: Adding Coolant On/Off button to main dialog
Hi Tom,
 The Coolant Off button is from my original 2 button coolant pump control. I would like to remove the Coolant Off button, or as you suggested earlier, to toggle the coolant on and off with a checkbox and do away with both coolant buttons.

Curious for future information...
 by using this code....
void CKMotionCNCDlg::OnBnClickedOnCoolant()
{
    Interpreter->InvokeAction(8,FALSE); // do the defined action for M8 Code
}

can the Coolant Off button properties be set as Visible False? And the coolant On button would toggle On and Off without any possible issues?

Thanks,
Troy
Group: DynoMotion Message: 10761 From: tmday7 Date: 1/2/2015
Subject: Re: Adding Coolant On/Off button to main dialog
Hit Tom,
 It did not sink in till this morning that the code you posted ....
void CKMotionCNCDlg::OnBnClickedOnCoolant()
{
    Interpreter->InvokeAction(8,FALSE); // do the defined action for M8 Code
}
is what i was using already with my 2 button coolant control. So ignore my previous post.

Iam now trying to toggle coolant pump with 1 button. But you suggested that a Check box would be easier. So i think i will go this route instead and eliminate more buttons on dialog face.

Troy
Group: DynoMotion Message: 10762 From: eric_kato_sanders Date: 1/3/2015
Subject: Re: Adding Coolant On/Off button to main dialog
I see what is going on.  We also need to add the message handler for the button click
ON_BN_CLICKED(IDC_OnCoolant, OnBnClickedOnCoolant)
Add this where you see the other ON_BN_CLICKED entries.
Group: DynoMotion Message: 10767 From: tmday7 Date: 1/4/2015
Subject: Re: Adding Coolant On/Off button to main dialog
Hi Eric,
 Per Toms suggestions and yours, iam going to try doing this (toggle coolant) with a check box instead of a button.  Ill start a new thread for it.

Thanks again,
Troy